package com.paginasenlanube.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.paginasenlanube.model.Header;
import com.paginasenlanube.model.SocialFeed;
import com.paginasenlanube.model.Website;
import com.paginasenlanube.service.SiteConfigService;
import com.paginasenlanube.view.Config;

@Controller
@Scope("request")
public class BuildSiteController {
	

	@Autowired
	 SiteConfigService configServices;
	
	@Autowired
	   private Website website;
	
	@Autowired
	   private Config cfg;
	
	
	public SiteConfigService getConfigService() {
		return configServices;
	}
	

	@RequestMapping("/dominio")
    public ModelAndView getDominio() {
  
        return new ModelAndView("paso1", "dominio", "info");
    }
	 
	 @RequestMapping("/contenidos")
	    public ModelAndView getContenido(@RequestParam String id) {
		 
			//UserDetails userDetails =(UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			 
			//User user =  configServices.getUser(userDetails.getUsername(), userDetails.getPassword());
		  configServices.getModel(Long.parseLong(id));
		  
		
		 
		  ModelAndView mav = new ModelAndView();
	      mav.addObject("website",website);
	      SocialFeed socialFeed = new SocialFeed();
	      Header header = new Header();
	      mav.addObject("socialFeed",socialFeed);
	      mav.addObject("header",header);
	 	  mav.addObject("config",cfg);
	 	  mav.setViewName("paso2/paso2");
	      return mav;
	        
	    }
	 
	 
	 @RequestMapping(value = "/ajax/page/update") 
	 public @ResponseBody String updatePage(@RequestParam("pageData") String json) {
		 configServices.updatePage(json);
		 return "success";
	
	 }
	 
	 @RequestMapping(value = "/ajax/page/new",produces="application/json") 
	 public @ResponseBody String addPage(@RequestParam("pageData") String json) {
		 String pageJson = configServices.addPage(json); 
		 return pageJson;
	 }
	  
	 @RequestMapping(value = "/ajax/palette") 
	 public @ResponseBody String updatePalette(@RequestParam("palette") String palette) {
		 configServices.updateDesignPalette(palette);
		 return cfg.toString();
	 }

	 @RequestMapping(value = "/ajax/template") 
	 public @ResponseBody String updateTemplate(@RequestParam("template") String template) {
		 configServices.updateDesignTemplate(template);
		 return cfg.toString();
	 }
	 
	 @RequestMapping(value = "/ajax/bgTheme") 
	 public @ResponseBody String updateTheme(@RequestParam("theme") String theme) {
		 configServices.updateDesignTheme(theme); 
		 return cfg.toString();
	 }
	 
	 @RequestMapping(value = "/ajax/bgThemeType") 
	 public @ResponseBody String updateThemeType(@RequestParam("bgtype") String bgtype) {
		 configServices.updateDesignThemeType(bgtype); 
		 return cfg.toString();
	 }
	 
	 @RequestMapping(value = "ajax/header", method = RequestMethod.POST)
	 public @ResponseBody String updateHeader(@ModelAttribute("header")
	                            Header header, BindingResult result) {
	         	        String returnText;
	        	        if(!result.hasErrors()){
	        	        	configServices.updateHeader(header.getName());
	        	            returnText = "Data has been added.";
	        	        }else{
	        	            returnText = "Sorry, an error has occur. Data has not been added.";
	        	        }
	        	        return returnText;
	    }
	 
	 @RequestMapping(value = "ajax/social", method = RequestMethod.POST)
	 public @ResponseBody String updateSocial(@ModelAttribute("socialFeed")
     						SocialFeed socialFeed, BindingResult result) {
	         	        String returnText;
	        	        if(!result.hasErrors()){
	        	        	configServices.updateSocial(socialFeed);
	        	            returnText = "Data has been added.";
	        	        }else{
	        	            returnText = "Sorry, an error has occur. Data has not been added.";
	        	        }
	        	        return returnText;
	    }

}
